#include"rss.h"
using std::istringstream;
using std::ostringstream;
RSS::RSS(size_t capa)
    {
        _rss.reserve(capa);
    }

    //读文件
void RSS::read(const string &filename)
    {
        XMLDocument doc;
        doc.LoadFile(filename.c_str());
        if(doc.ErrorID())
        {
            std::cerr << "loadFile fail" << endl;
            return;
        }

        XMLElement *itemNode = 
            doc.FirstChildElement("rss")->FirstChildElement("channel")->FirstChildElement("item");
        while(itemNode)
        {
            string title = itemNode->FirstChildElement("title")->GetText();
            string link=  itemNode->FirstChildElement("link")->GetText();
            string description;
            if(itemNode->FirstChildElement("description")){
                description=itemNode->FirstChildElement("description")->GetText();
            }
            string content; 
            if(itemNode->FirstChildElement("content:encoded")){
                content=itemNode->FirstChildElement("content:encoded")->GetText();
            }
            
            std::regex reg("<[^>]+>");//通用正则表达式
            if(description!=string()){
                description = regex_replace(description, reg, "");
            }

            if(content!=string()){
                content = regex_replace(content, reg, "");
            }
            RSSIteam rssItem;
            rssItem._title = title;
            rssItem._link = link;
            rssItem._description = description;
            rssItem._content=content;
            _rss.push_back(rssItem);
            itemNode = itemNode->NextSiblingElement("item");
        }
    }

    //写文件
void RSS::store(const string &filename,int &pbidx,size_t &off,map<int,pair<int,int>> &offlib)
    {
        ofstream ofs(filename,std::ios_base::app);
        if(!ofs)
        {
            std::cerr << "open " << filename << " fail!" << endl;
            return;
        }

        for(size_t idx = 0; idx != _rss.size(); ++idx)
        {
            std::stringstream ss;
            ss << "<doc>\n\t<docid>" << pbidx+idx + 1
                << "</docid>\n\t<title>" << _rss[idx]._title<<"</title>\n" 
                << "\t<link>" << _rss[idx]._link<<"</link>\n";
                if(_rss[idx]._description!=string()&&isSpace(_rss[idx]._description)) {
                ss<< "\t<description>" << _rss[idx]._description<<"</description>\n";
                }
                if(_rss[idx]._content!=string()&&isSpace(_rss[idx]._content)){
                ss<< "\t<content>"<< _rss[idx]._content<<"</content>\n";
                }
                ss<< "</doc>\n";
            ss << '\n';
            ofs.write(ss.str().c_str(),ss.str().size());
            offlib.insert({pbidx+idx+1,{off,ss.str().size()}});
            off+=ss.str().size();
        }
        pbidx+=(int)_rss.size();

        ofs.close();
    }
bool RSS::isSpace(string str){
    for(auto ele:str){
        if(ele!=' '){
            return true;
        }
    }
    return false;
}